﻿Imports Mystery

Public Class ContentList
    Implements IContentShower


    Private _created As Boolean = False
    Private _content_type As Type

    Private _items_map As New Dictionary(Of IContent, ListBoxItem)
    Private _items_content As New Dictionary(Of ListBoxItem, IContent)
    Private _content As IContent
    Private button_attr As ContentButtonAttribute

    Private Sub addContentInList(c As IContent)
        If _items_map.ContainsKey(c) Then Exit Sub 'already here


        Dim li As New ListBoxItem
        _items_map(c) = li
        _items_content(li) = c
        Dim sp As New StackPanel
        sp.Orientation = Orientation.Horizontal
        li.Content = sp
        Dim cb As New ContentLabel(c)
        Dim icb As IContentButton = button_attr.createButton(c)
        AddHandler li.Selected, AddressOf icb.Press
        cb.Content = icb.getText
        cb.MinWidth = 100
        Dim image As Image = icb.getImage
        image.Height = 25
        image.Width = 25
        sp.Children.Add(image)
        sp.Children.Add(cb)
        contentListControl.Items.Add(li)

    End Sub

    ''' <summary>
    ''' create the list
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <param name="contents">if nothin all of a type will be displayed</param>
    ''' <remarks></remarks>
    Public Sub create(Of T As IContent)(contents As IEnumerable(Of T))
        If _created Then Exit Sub 'it shall not create it twice
        _content_type = GetType(T)

        Dim all As IEnumerable(Of IContent)
        If contents Is Nothing Then
            all = _content_type.getMysteryAttribute(Of ContentListContentGatherAttr)().retriveContents
        Else
            all = contents
        End If


        button_attr = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentButtonAttribute, T)()


        For Each c As IContent In all
            addContentInList(c)
        Next

    End Sub

    Public Function show(content_to_show As Mystery.IContent) As Boolean Implements IContentShower.show
        If content_to_show Is Nothing Then Return False
        If content_to_show.GetType() IsNot _content_type Then Return False
        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()

        If _content IsNot Nothing AndAlso _content.Equals(content_to_show) Then
            'it means this control it is already showing the content, but maybe it is not focus
            If Not ir.showen_type = _content_type Then Return ir.showType(_content_type, force_reload:=False)
        End If

        _content = content_to_show

        Me.Dispatcher.Invoke(Sub()
                                 Dim cva As ContentViewAttribute = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentViewAttribute)(_content_type)

                                 contentbox.Children.Clear()
                                 contentbox.Children.Add(cva.createControl(content_to_show))

                                 If Not _items_map.ContainsKey(_content) Then addContentInList(_content)
                                 _items_map(_content).IsSelected = True

                                 ir.showen_content = _content
                             End Sub)

        Return True

    End Function

    Private Sub UserControl_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.GotFocus
        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()
        ir.showen_type = _content_type
        ir.showen_content = _content
    End Sub

    Public ReadOnly Property showen_content As Mystery.IContent Implements IContentShower.showen_content
        Get
            Return _content
        End Get
    End Property

    Public Sub DropContent(content As Mystery.IContent) Implements IContentShower.DropContent
        If Not _items_map.ContainsKey(content) Then Exit Sub 'nothing to do
        Me.Dispatcher.Invoke(Sub()
                                 Dim li As ListBoxItem = _items_map(content)
                                 contentListControl.Items.Remove(li)
                                 If _content IsNot Nothing AndAlso _content.guid = content.guid Then
                                     contentbox.Children.Clear()
                                     _content = Nothing
                                 End If
                             End Sub)
        
    End Sub
End Class
